package com.test.springCloud.utils;

import com.alibaba.fastjson.JSONObject;

/**
 * @author 作者:LuoXingFa
 *
 * @version 创建时间:2022年7月12日{time}
 *
 * @Description 类说明
 */
public class ResultUtils {

    private int code;// 状态码
    private String msg;
    private int count;// 做分页的
    private Object data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    // 私有构造
    private ResultUtils() {
    }

    // 查询成功的返回
    public static ResultUtils getSuccess(String msg, Object data) {
        ResultUtils resultUtils = new ResultUtils();
        resultUtils.setMsg(msg);
        resultUtils.setData(data);
        return resultUtils;
    }

    // 增删改成功后没有返回值
    public static ResultUtils getSuccess(String msg) {
        return ResultUtils.getSuccess(msg, null);
    }

    // 失败后的方法
    public static ResultUtils getFail(String msg) {
        ResultUtils resultUtils = new ResultUtils();
        resultUtils.setCode(500);
        resultUtils.setMsg(msg);
        return resultUtils;
    }

    public static JSONObject jsonFail(String msg) {
        JSONObject res = new JSONObject();
        res.put("code", 500);
        res.put("msg", msg);
        return res;
    }

    public static JSONObject jsonFail(String msg, Integer code) {
        JSONObject res = new JSONObject();
        res.put("code", 500);
        res.put("msg", msg);
        return res;
    }

    public static JSONObject jsonSuccess(String msg, Object data) {
        JSONObject res = new JSONObject();
        res.put("code", 200);
        res.put("msg", msg);
        res.put("data", data);
        return res;
    }

    public static JSONObject jsonSuccessNotData(String msg) {
        JSONObject res = new JSONObject();
        res.put("code", 200);
        res.put("msg", msg);
        return res;
    }

    public static JSONObject jsonSuccess(Object data) {
        JSONObject res = new JSONObject();
        res.put("code", 200);
        res.put("data", data);
        return res;
    }

}
